package bean.back.deletepark;

import com.alibaba.fastjson.JSON;
import util.JDBCConnection;

import java.sql.ResultSet;

public class BackDeleteparkBean {
    int code;
    String message;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String delete(String token,String id){
        JDBCConnection jdbc = new JDBCConnection();
        String tokenArry[] = token.split("#");
        try {
            if(checkPermissions(tokenArry[0],tokenArry[2],jdbc)){
               String deleteSQL = "delete from parkinglot where lotid = '"+id+"'";
               jdbc.executeUpdate(deleteSQL);
               message="成功";
               code=0;
            }
            else {
                code = 200;
                message = "token错误或者用户无权限";
            }
        }catch (Exception e){
            code = 100;
            message = "token错误";
        }
        return JSON.toJSONString(this);
    }

    private boolean checkPermissions(String operator_id, String operator_pwd, JDBCConnection jdbc){
        String querySQL = "select backpsd from backstage where backaccount = '"+operator_id+"'";
        ResultSet rs = jdbc.executeQuery(querySQL);
        try {
            rs.next();
            if(operator_pwd.equals(rs.getString("backpsd"))) return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
